package com.keyboardr.gamerater;

import java.io.Serializable;

import org.json.JSONException;
import org.json.JSONObject;

public class Player implements JSONable, Serializable {
	private static final long serialVersionUID = -7456087283034049535L;
	public static final int FIDE_SWITCH_POINT = 2400; // Rating at which k
														// factor changes under
														// FIDE rules
	public static final int NUM_GAMES = 30; // Number of games before k factor
											// changes under FIDE rules
	public static final int USCF_HIGH = 2400;
	public static final int USCF_LOW = 2100;
	private double kFactor;
	private double rating;
	private int numGamesPlayed;
	private boolean permanentK;
	public String firstName;
	public String lastName;

	public Player(String first, String last, int rate, double k,
			boolean permanentK, int gamesPlayed) {
		firstName = first;
		lastName = last;
		rating = rate;
		kFactor = k;
		this.permanentK = permanentK;
		numGamesPlayed = gamesPlayed;
	}

	public Player(JSONObject jObject) {
		try {
			firstName = jObject.getString("firstName");
			lastName = jObject.getString("lastName");
			rating = jObject.getInt("rating");
			kFactor = jObject.getDouble("kFactor");
			permanentK = jObject.getBoolean("permanentK");
			numGamesPlayed = jObject.getInt("numGamesPlayed");
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}

	public Player(String first, String last, int rate) {
		this(first, last, rate, 25.0, false, 0);
	}

	public Player(String first, String last) {
		this(first, last, 1300, 25.0, false, 0);
	}

	public void playAGame(double d, boolean fide) {
		numGamesPlayed++;
		rating = d;
		if (fide) {
			if (!permanentK) {
				if (rating >= FIDE_SWITCH_POINT) {
					if (numGamesPlayed >= NUM_GAMES) {
						permanentK = true;
						kFactor = 10;
					} else {
						kFactor = 15;
					}
				} else {
					kFactor = 25;
				}
			}
		} else {
			if (rating > USCF_HIGH) {
				kFactor = 16;
			}
			if (rating <= USCF_HIGH) {
				kFactor = 24;
			}
			if (rating <= USCF_LOW) {
				kFactor = 32;
			}
		}
	}

	/**
	 * @return the kFactor
	 */
	public double getkFactor() {
		return kFactor;
	}

	/**
	 * @param kFactor the kFactor to set
	 */
	public void setkFactor(double kFactor) {
		this.kFactor = kFactor;
	}

	/**
	 * @return the rating
	 */
	public double getRating() {
		return rating;
	}

	/**
	 * @param rating the rating to set
	 */
	public void setRating(int rating) {
		this.rating = rating;
	}

	@Override
	public String toString() {
		return firstName + " " + lastName;
	}

	@Override
	public boolean equals(Object other) {
		if (!(other instanceof Player)) {
			return false;
		}
		Player player = (Player) other;
		return (this.firstName.equals(player.firstName) && this.lastName
				.equals(player.lastName));
	}

	@Override
	public JSONObject toJSON() {
		JSONObject jObject = new JSONObject();
		try {
			jObject.put("kFactor", kFactor);
			jObject.put("rating", rating);
			jObject.put("numGamesPlayed", numGamesPlayed);
			jObject.put("permanentK", permanentK);
			jObject.put("firstName", firstName);
			jObject.put("lastName", lastName);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return jObject;
	}
}
